#!/bin/bash

# save state of clipboard
clipboard=$(xclip -sel clip -o)
paste () {
    echo -n "$clipboard"
}

# if clipboard contains a megal url
[[ $(paste | grep mega | grep http) != *"mega"* ]] && (echo "error url does not contain mega download link"; exit 1)
# extract some name and store it there
dir=$(paste | rev | cut -d"/" -f1 | rev | cut -d"#" -f1)
echo "downloading $(paste) and putting it in $dir"
mkdir -p $dir
cd $dir || (echo "failed to cd"; exit 2)
megadl $(paste) || (echo "failed downloading $(paste)"; exit 3)
echo "downloaded $(paste)"
exit
